<template>
  <el-container id="app">
    <el-header></el-header>
    <el-main>
      <div class="title">
        <h2><i class="el-icon-goods"></i>盲盒在线交易·BlindBoxTesco·一个更加方便的平台</h2>
      </div>
      <el-row style="height: 600px;overflow-y: hidden;overflow-x: hidden;position:relative;left:75px;">
        <!-- 注册 -->
        <el-col :span="12">
          <div :class="changeRegisterCss == 1 ? 'register_public register_1' : 'register_public register_0'"> 
            <h1 style="border-right: 2px solid black; padding: 5px;">用户注册</h1>
            <el-form ref="formRegister" :model="formRegister" :rules="formRegisterRules" label-width="80px">
              <el-form-item label="用户名" prop="username" style="font-weight: bolder;">
                <el-input v-model="formRegister.username" placeholder="请输入您的用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password" style="font-weight: bolder;">
                <el-input type="password" v-model="formRegister.password" placeholder="请输入您的密码" show-password></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="passwordAgain" style="font-weight: bolder;">
                <el-input type="password" v-model="formRegister.passwordAgain" placeholder="请再次输入您的密码" show-password></el-input>
              </el-form-item>
              <el-form-item label="性别" style="font-weight: bolder;">
                <el-radio-group v-model="formRegister.gender">
                  <el-radio label="男" style="font-weight: bolder;"></el-radio>
                  <el-radio label="女" style="font-weight: bolder;"></el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="手机号码" prop="phoneNumber" style="font-weight: bolder;">
                <el-input v-model="formRegister.phoneNumber" placeholder="请输入您的手机号码"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit_register('formRegister')">立即注册</el-button>
                <el-button @click="resetForm('formRegister')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <!-- 登录 -->
        <el-col :span="12">
          <div :class="changeRegisterCss == 1 ? 'login_public login_1' : 'login_public login_0'">
            <h1 style="border-right: 2px solid black; padding: 5px;">用户登陆</h1>
            <el-form ref="formLogin" :model="formLogin" :rules="formLoginRules" label-width="80px">
              <el-form-item label="用户名" prop="username" style="font-weight: bolder;margin-top: 125px;">
                <el-input v-model="formLogin.username" placeholder="请输入您的用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password" style="font-weight: bolder;">
                <el-input type="password" v-model="formLogin.password" placeholder="请输入您的密码" show-password></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit_login('formLogin')">立即登录</el-button>
                <el-button @click="resetForm('formLogin')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-row style="height: 404px;">
          <el-col :class="changeRegisterCss == 1 ? 'change_public change_1' : 'change_public change_0'">
            <div>
              <el-button @click="changeRegisterClass(changeRegisterCss)" style="height:80px;background-color:rgba(255, 255, 255, 0.5);border:2px solid white;border-radius:10px;"><h1>{{changeButtonValue}}</h1></el-button>
            </div>
          </el-col>
        </el-row>
        <el-link :class="changeRegisterCss == 1 ? 'toHomeCss_1' : 'toHomeCss_0' " @click="toHome()" :underline="false" style="font-weight: bolder;">{{changeToHomeValue}}</el-link>
      </el-row>
    </el-main>
    <el-footer></el-footer>
  </el-container>
</template>

<script>
export default {
  inject: ['reload'],
  data() {
    // 验证手机号的规则
    var checkPhoneNumber = (rule, value, cd) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[789]|18[0-9]|14[57])[0-9]{8}$/
      if (regMobile.test(value)) {
        return cd()
      }
      cd(new Error('请输入正确的手机号'))
    }
    // 再次输入
    var checkPasswordAgain = (rule, value, cd) => {
      if (this.formRegister.password == this.formRegister.passwordAgain){
        return cd()
      }
      cd(new Error("两次密码不一致"))
    }
    return {
      changeRegisterCss: 1,
      changeButtonValue: "没有账号？立即注册",
      changeToHomeValue: "先不登录，返回主页",
      formRegister: {
        username: "",
        password: "",
        passwordAgain: "",
        gender: "男",
        phoneNumber: ""
      },
      formLogin: {
        username: "",
        password: ""
      },
      formRegisterRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
          ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
          ],
        passwordAgain: [
          { required: true, message: '请再次输入密码', trigger: 'blur' }, 
          { validator: checkPasswordAgain }
          ],
        phoneNumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' }, 
          { validator: checkPhoneNumber }
          ]
      },
      formLoginRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
          ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
          ]
      }
    }
  },
  methods: {
    changeRegisterClass(index){
      // console.log(index)
      if(index == 1){
        this.changeRegisterCss = 0
        this.changeButtonValue = "已有账号？立即登录"
        this.changeToHomeValue = "先不注册，返回主页",
        this.$refs['formRegister'].resetFields();
      }else{
        this.changeRegisterCss = 1
        this.changeButtonValue = "没有账号？立即注册"
        this.changeToHomeValue = "先不登录，返回主页",
        this.$refs['formLogin'].resetFields();
      }
    },
    onSubmit_register(formName) {
      var that = this;
      this.$refs[formName].validate((valid) =>{
        if(valid){
          this.$axios.post("/user/register", this.formRegister,{timeout:2000})
          .then(function(response){
            // console.log(response)
            var data = response.data;
            if(data.flag == true){
              that.$message({
                showClose: true,
                message: data.msg,
                type: "success"
              })
              that.reload()
            }else{
              that.$message({
                showClose: true,
                message: data.msg,
                type: "error"
              })
            }
          })
          .catch(err => {
            that.$message({
              showClose: true,
              message: "系统未知错误，请联系管理员",
              type: "error"
            })
          })
        }else{
          that.$message({
            showClose: true,
            message: "请完善信息再点击注册",
            type: "error"
          })  
        }
      })
    },
    onSubmit_login(formName){
      var that = this;
      this.$refs[formName].validate((valid) =>{
        if(valid){
          this.$axios.post("/user/login", this.formLogin,{timeout:2000})
          .then(function(response){
            // console.log(response.data)
            var data = response.data;
            if(data.flag == true){
              that.$message({
                showClose: true,
                message: data.msg,
                type: "success"
              })
              localStorage.setItem("loginToken",response.data.data)
              that.$router.push('/')
            }else{
              that.$message({
                showClose: true,
                message: data.msg,
                type: "error"
              })
            }
          })
          .catch(err => {
            that.$message({
              showClose: true,
              message: "系统未知错误，请联系管理员",
              type: "error"
            })
          })
        }else{
          this.$message({
            showClose: true,
            message: "请输入正确的用户名和密码，再点击登陆",
            type: "error"
          })  
        }
      })
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
    },
    toHome(){
      this.$router.push("/")
    }
  }
}
</script>

<style lang="less" scoped>
.change_public{
  width: 250px;
  height: 404px;
  border: 2px solid white;
  text-align: center;
  line-height: 400px;
  border-radius: 15px;
  position:relative;
  background-color: rgba(255, 255, 255, 0);
  z-index: 2;
}
.change_0{
  top: -400px;
  animation: change_register 0.5s;
}
.change_1{
  top: -404px;
  animation: change_login 0.5s;
}
@keyframes change_register{
  0% {right: 404px; top: -400px;}
  100%{right: 0px; top: -400px;}
}
@keyframes change_login{
  0% {right: -404px; top: -404px;}
  100%{right: -1px; top: -404px;}
}
.register_public{
  width: 400px;
  height: 400px;
  border-radius: 15px;
  margin-top: 150px;
  display: flex; 
  line-height: 400px;
  text-align: center;
  position:relative;
  z-index: 1;
}
.register_0 {
  border: white 2px solid;
  background-color: rgba(255, 255, 255, 0.9);
  animation: register_hidden_in 0.5s;
}
.register_1 {
  background-color: rgba(255, 255, 255, 0);
  top: -600px;
  animation: register_hidden_out 0.5s;
}
@keyframes register_hidden_out{
  0% {right: 0px; top: 0px; background-color: rgba(255, 255, 255, 0.9);}
  100%{right: 0px; top: 600px; background-color: rgba(255, 255, 255, 0);}
}
@keyframes register_hidden_in{
  0% {right: 500px; top: 0px; background-color: rgba(255, 255, 255, 0);}
  100%{right: 0px; top: 0px; background-color: rgba(255, 255, 255, 0.9);}
}
.login_public{
  width: 400px;
  height: 400px;
  border-radius: 15px;
  margin-top: 150px;
  display: flex; 
  line-height: 400px;
  text-align: center;
  z-index: 1;
}
.login_0 {
  background-color: rgba(255, 255, 255, 0.9);
  position:relative;
  right: -1px;
  top: -604px;
  animation: login_hidden_out 0.5s;
}
.login_1 {
  border: white 2px solid;
  background-color: rgba(255, 255, 255, 0.9);
  position:relative;
  right: 154px;
  animation: login_hidden_in 0.5s;
}
@keyframes login_hidden_out{
  0% {right: 154px; top: 0px; background-color: rgba(255, 255, 255, 0.9);}
  100%{right: 154px; top: 600px; background-color: rgba(255, 255, 255, 0);}
}
@keyframes login_hidden_in{
  0% {right: -300px; top: 0px; background-color: rgba(255, 255, 255, 0);}
  100%{right: 154px; top: 0px; background-color: rgba(255, 255, 255, 0.9);}
}
.toHomeCss_0{
  border: 2px solid white;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.5);
  padding: 5px;
  position:relative;
  top: -36px;
  left: -202px;
  animation: change_to_home_register 0.5s;
  z-index: 2;
}
.toHomeCss_1{
  border: 2px solid white;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.5);
  padding: 5px;
  position:relative;
  top: -40px;
  left: -402px;
  animation: change_to_home_login 0.5s;
  z-index: 2;
}
@keyframes change_to_home_login{
  0% {left: 0px; top: -40px;}
  100%{left: -402px; top: -40px;}
}
@keyframes change_to_home_register{
  0% {left: -605px; top: -36px;}
  100%{left: -202px; top: -36px;}
}
.el-container{
  background-color: rgba(83, 153, 83, 0.5);
  display: flex; 
  flex-direction: column;
}
.el-header .el-footer{
  background-color: rgba(62, 128, 91, 0.5);
  text-align: center;
  height: auto;
}
.el-main{
  border-radius: 20px;
  width: auto;
  text-align: center;
  display: flex; 
  justify-content: center;
  border: white 5px solid;
  margin: 20px;
}
.el-form-item {
  margin-top: 20px;
}
.title{
  position:absolute;
  display: flex; 
  flex-direction: column;
  left: 660px;
}
.el-input{
  width: 200px;
}
</style>